package com.jy.qnbbs.controller;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
public class ConsomerController {


    @Autowired
    private LoadBalancerClient loadBalancerClient;


    /** 获取提供者环境名 */
    @GetMapping("/getProfilesName")
    public String getProfilesName(){
        ServiceInstance si=loadBalancerClient.choose("qnbbs-provider");
        StringBuffer url=new StringBuffer();
        url.append("http://");
        url.append(si.getHost());
        url.append(":");
        url.append(si.getPort());
        url.append("/getProfilesNameConfig");
        RestTemplate rt=new RestTemplate();
        ParameterizedTypeReference<String> rtr=new ParameterizedTypeReference<String>(){};
        ResponseEntity<String> re=rt.exchange(url.toString(), HttpMethod.GET,null,rtr);
        return re.getBody();

    }

    /** 获取讨论版区 */
    @GetMapping("/getBbsSortList")
    public String getBbsSortList(){

        ServiceInstance si=loadBalancerClient.choose("qnbbs-provider");
        StringBuffer url=new StringBuffer();
        url.append("http://");
        url.append(si.getHost());
        url.append(":");
        url.append(si.getPort());
        url.append("/getBbsSortList");

        RestTemplate rt=new RestTemplate();
        ParameterizedTypeReference<Object> rtr=new ParameterizedTypeReference<Object>(){};
        ResponseEntity<Object> re=rt.exchange(url.toString(), HttpMethod.GET,null,rtr);
        String body=JSON.toJSONString(re.getBody());
        return body;
    }

}
